<div class="gn_box">

<div id="gn-widget-head-7" class="gn_box_head">
  <div class="gn_box_head_title" style="padding-left:0;color:#000;">
     <a name="polls"></a>
     <?php echo image_tag('icons/graph-bar-links-icone-5840-16.png', array('align'=>"absmiddle"))?>&nbsp;
  	 Санал асуулга    	 
  </div>
</div>
  

<?php
$polls = Doctrine::getTable('Poll')->getPolls($leagueId, 1, 1);
$voteIds = $sf_user->getAttribute('voteIds', array());
?>

<?php if(sizeof($polls)):?>

<div class="tabs" id="tabs-polls">
  <!--<?php //$i=0; foreach($polls as $poll):?>
    <div class="tab <?php //if($i++ == 0) echo 'active'?>" id="tab-<?php //echo $poll->getId()?>"><?php //echo $i?></div>
  <?php //endforeach?>-->
  
  <!--<hr class="separator"/>-->

  <?php $i=0; foreach($polls as $poll):?>
    <?php 
      $pollId = $poll->getId();
      $isVoted = sizeof($voteIds) ? in_array($pollId, $voteIds) : false;
    ?>
    <div class="c <?php if($i++ == 0) echo 'active'?>" id="tab-<?php echo $pollId?>-c">

      <form id="pollForm<?php echo $pollId?>" method="POST">
      
         <input type="hidden" value="<?php echo $pollId?>" name="poll_id" id="poll_id" />

         <p><?php echo $poll?></p>  				     

		     <?php $options = Doctrine::getTable('PollOption')->findByPollId($pollId);?>

		     <!-- Result -->
		     <div id="pollResult<?php echo $pollId?>" style="display:<?php echo $isVoted ? 'block' : 'none'?>;">
		       <?php $votes_array = $poll->getVotes();
		         foreach ($options as $option)
		         {
		           echo '<label style="margin:8px;display:block;"><span style="color:#000;">'.$votes_array[$option->getId()]."</span> - ".$option.'</label>';
		         }
		       ?>
		       <a class="right" onclick="togglePoll(<?php echo $pollId?>);" style="text-decoration:underline;"><?php echo ('Үр дүн хаах')?></a>
 		       <br clear="all">
		     </div>
		     
		     <!-- Choices -->
		     <div id="pollChoices<?php echo $pollId?>" style="display:<?php echo $isVoted ? 'none' : 'block'?>;">
  			     <?php foreach ($options as $option):?>
                <label style="margin:8px;display:block;">
                    <input type="radio" name="options[]" align="absmiddle" value="<?php echo $option->getId() ?>" />
                    <?php echo $option ?>
                </label>
             <?php endforeach;?>
             
             <!-- Buttons -->
             <?php if(!$isVoted):?>
                 <a href="#polls" onclick="submitPollForm(<?php echo $pollId?>);" style="padding:5px 10px;background:#ECE8DA;border:1px solid #D4CCB0;-moz-border-raduis:5px;-webkit-border-raduis:5px;border-raduis:5px;">Санал өгөх</a>
             <?php else:?>
  	             <p style="margin:10px 0;color:#000;"><?php echo ('Та саналаа өгсөн байна.')?></p>
             <?php endif?>
             
             <a class="right" onclick="togglePoll(<?php echo $pollId?>);" style="text-decoration:underline;"><?php echo ('Үр дүн харах')?></a>
             <br clear="all">
         </div><!--pollChoices-->

	     </form>

	  </div><!--c-->
  <?php endforeach?>

</div><!--tabs-->


<script type="text/javascript">
function submitPollForm(pollId)
{
   formId = '#pollForm' + pollId;
   tabId = '#tab-' + pollId + '-c';
   voteMessage = '#voteMessage' + pollId;
   
    $.ajax({
      url: "<?php echo url_for('content/vote')?>", 
      type: 'POST',
      data: $(formId).serialize(),
      success: function(data){
        $(tabId).html(data);
        $(voteMessage).show();
        $(voteMessage).fadeOut(10000);
    }
  });
  return false;
}


function togglePoll(pollId)
{
   result = '#pollResult' + pollId;
   choices = '#pollChoices' + pollId;
   $(result).toggle();
   $(choices).toggle();
}
</script>

<?php endif?>

</div><!--gn_box-->